package com.iOS.service.impl;

import com.iOS.dao.LoginRecordMapper;
import com.iOS.dao.UserInfoMapper;
import com.iOS.dto.UserInfoExecution;
import com.iOS.entity.LoginRecord;
import com.iOS.entity.UserInfo;
import com.iOS.enums.UserInfoEnum;
import com.iOS.service.IUserInfoService;
import com.iOS.util.ImageUtil;
import com.iOS.util.PathUtil;
import com.iOS.util.TokenUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.io.InputStream;
import java.util.Date;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private LoginRecordMapper loginRecordMapper;

    @Override
    public UserInfoExecution Register(String username, String password_1, String password_2) {
        try {
            if (!password_1.equals(password_2)) {
                return new UserInfoExecution(UserInfoEnum.PASSWORD_ERROR);
            } else {
                if (userInfoMapper.checkUsername(username) == 1) {
                    return new UserInfoExecution(UserInfoEnum.REPEAT_USERNAME);
                } else if (userInfoMapper.checkUsername(username) == 0) {
                    /*增加一条用户记录*/
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUsername(username);
                    userInfo.setPassword(password_1);
                    userInfo.setCreateTime(new Date());
                    userInfo.setUpdateTime(new Date());
                    userInfoMapper.insert(userInfo);
                    /*生成token*/
                    LoginRecord loginRecord = new LoginRecord();
                    loginRecord.setUserId(userInfoMapper.selectByUsername(username).getId());
                    loginRecord.setCreateTime(new Date());
                    loginRecord.setToken(TokenUtil.GetGUID());
                    loginRecordMapper.insert(loginRecord);
                    return new UserInfoExecution(UserInfoEnum.SUCCESS, loginRecord);
                }
            }
        } catch (Exception e) {
            return new UserInfoExecution(UserInfoEnum.INNER_ERROR);
        }
        return new UserInfoExecution(UserInfoEnum.FAIL);
    }

    @Override
    public UserInfoExecution Login(String username, String password) {
        try {
            if (userInfoMapper.checkUsername(username) == 0) {
                return new UserInfoExecution(UserInfoEnum.ERROR_USERNAME);
            } else if (userInfoMapper.checkUsername(username) == 1) {
            /*获取用户信息*/
                UserInfo userInfo = new UserInfo();
                userInfo = userInfoMapper.selectByUsername(username);
                if (userInfo.getPassword().equals(password)) {
                /*更新token*/
                    LoginRecord loginRecord = new LoginRecord();
                    loginRecord.setToken(TokenUtil.GetGUID());
                    loginRecord.setCreateTime(new Date());
                    loginRecord.setUserId(userInfoMapper.selectByUsername(username).getId());
                    loginRecordMapper.update(loginRecord);
                    return new UserInfoExecution(UserInfoEnum.SUCCESS, loginRecord);
                } else {
                    return new UserInfoExecution(UserInfoEnum.ERROR_PASSWORD);
                }
            }
        } catch (Exception e) {
            return new UserInfoExecution(UserInfoEnum.INNER_ERROR);
        }
        return new UserInfoExecution(UserInfoEnum.FAIL);
    }

    @Override
    public UserInfoExecution GetUserInfo(String token) {
        try {
            if (loginRecordMapper.checkToken(token) == 0) {
                return new UserInfoExecution(UserInfoEnum.ERROR_TOKEN);
            } else if (loginRecordMapper.checkToken(token) == 1) {
                LoginRecord loginRecord = new LoginRecord();
                loginRecord = loginRecordMapper.selectByToken(token);
                if (loginRecord.getToken().equals(token)) {
                    UserInfo userInfo = new UserInfo();
                    userInfo = userInfoMapper.selectById(loginRecord.getUserId());
                    return new UserInfoExecution(UserInfoEnum.SUCCESS, userInfo);
                }
            }
        } catch (Exception e) {
            return new UserInfoExecution(UserInfoEnum.INNER_ERROR);
        }
        return new UserInfoExecution(UserInfoEnum.FAIL);
    }

    @Override
    public UserInfoExecution CompleteUserInfo(UserInfo userInfo, String token, InputStream HeadImgInputStream, String filename) {
        try {
            if (loginRecordMapper.checkToken(token) == 0) {
                return new UserInfoExecution(UserInfoEnum.ERROR_TOKEN);
            } else if (loginRecordMapper.checkToken(token) == 1) {
                LoginRecord loginRecord = new LoginRecord();
                loginRecord = loginRecordMapper.selectByToken(token);
                if (loginRecord.getToken().equals(token)) {
                    userInfo.setId(loginRecord.getUserId());
                    userInfo.setUpdateTime(new Date());
                    if (HeadImgInputStream.available() != 0) {
                        userInfo.setHeadView(userHeadImg(HeadImgInputStream, filename));
                    }
                    userInfoMapper.update(userInfo);
                    return new UserInfoExecution(UserInfoEnum.SUCCESS, userInfo);
                }
            }
        } catch (Exception e) {
            return new UserInfoExecution(UserInfoEnum.INNER_ERROR);
        }
        return new UserInfoExecution(UserInfoEnum.FAIL);
    }

    /*存储图片并返回路径*/
    private String userHeadImg(InputStream HeadImgInputStream, String filename) {
        /*获取头像存储目录*/
        String dir = PathUtil.getHeadImgPath();
        String savePath = ImageUtil.ThumbnailsImg(HeadImgInputStream, filename, dir);
        return savePath;
    }

}
